package fashionshop.gui.common.component;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import fashionshop.gui.common.event.CommandListener;

@SuppressWarnings("serial")
public class CommandToolBar extends JPanel {
	
	public static enum ToolBarType{
		ADD_EDIT_DELETE
	}
	
	private RoundButton addButton;
	private RoundButton editButton;
	private RoundButton deleteButton;
	private RoundButton refreshButton;
	private CommandListener listener;
	private ToolBarType type;
	private static CommandToolBar instance;
	
	public static CommandToolBar getInstance(){
		if(instance == null){
			instance = new CommandToolBar();
		}
		return instance;
	}
	
	public void buildToolBar(ToolBarType type, CommandListener listener){
		this.type = type;
		this.listener = listener;
		removeAll();
		switch(type){
		case ADD_EDIT_DELETE:
			add(addButton);
			add(editButton);
			add(deleteButton);
			break;
		default:
		}
		add(refreshButton);
	}

	public CommandListener getListener() {
		return listener;
	}

	public ToolBarType getType() {
		return type;
	}

	/**
	 * Create the panel.
	 */
	private CommandToolBar() {
		initComponents();
	}
	private void initComponents() {
		FlowLayout flowLayout = (FlowLayout) getLayout();
		flowLayout.setVgap(10);
		flowLayout.setHgap(10);
		flowLayout.setAlignment(FlowLayout.LEFT);
		
		this.addButton = new RoundButton(new ImageIcon(CommandToolBar.class.getResource("/images/add-image-48x48.png"))){{
			setPressedIcon(new ImageIcon(CommandToolBar.class.getResource("/images/add-image-48x48.png")));
			setRolloverIcon(new ImageIcon(CommandToolBar.class.getResource("/images/add-image-48x48.png")));
		}};
		this.addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listener != null){
					listener.addPerformed();
				}
			}
		});
		
		this.editButton = new RoundButton(new ImageIcon(CommandToolBar.class.getResource("/images/edit-image-48x48.png"))){{
			setPressedIcon(new ImageIcon(CommandToolBar.class.getResource("/images/edit-image-48x48.png")));
			setRolloverIcon(new ImageIcon(CommandToolBar.class.getResource("/images/edit-image-48x48.png")));
		}};
		this.editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listener != null){
					listener.editPerformed();
				}
			}
		});
		
		this.deleteButton = new RoundButton(new ImageIcon(CommandToolBar.class.getResource("/images/remove-image-48x48.png"))){{
			setPressedIcon(new ImageIcon(CommandToolBar.class.getResource("/images/remove-image-48x48.png")));
			setRolloverIcon(new ImageIcon(CommandToolBar.class.getResource("/images/remove-image-48x48.png")));
		}};
		this.deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listener != null){
					listener.deletePerformed();
				}
			}
		});
		
		this.refreshButton = new RoundButton(new ImageIcon(CommandToolBar.class.getResource("/images/refresh-image-48x48.png"))){{
			setPressedIcon(new ImageIcon(CommandToolBar.class.getResource("/images/refresh-image-48x48.png")));
			setRolloverIcon(new ImageIcon(CommandToolBar.class.getResource("/images/refresh-image-48x48.png")));
		}};
		this.refreshButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listener != null){
					listener.refreshPerformed();
				}
			}
		});
	}

}
